define(['zepto', 'utils', 'JST'], function($, utils, JST){
  return {
    init: function(_CTX_, temp) {
      this._CTX_ = _CTX_
      this.temp = temp
      this.modal = {}
      
      this.$agentWithdraw = $('[href="#agent-withdraw"]')
      this.$agentWithdraw.length ? this.bindPushAction() : void 0
    },

    bindPushAction: function() {

      var that = this

      // on agent withdraw
      $(document).on('click', '[href="#agent-withdraw"]', function(e) {
        e.preventDefault()
        var defer = that.compilewithdrawPage()

        $.when(defer).done(function($page) {
                $.push.transitionIn($page)
                
            })

      })

      // On agent withdraw
      this.$agentWithdraw.on('edited:profile:withdraw', function(e, $page, offer) {

        var itemDomString = $(JST.agentWithdrawTpl({
        	'offer': offer
        	}))

        if(that.temp) {
          $.toast("提交提现申请成功")
          setTimeout(function() {
                  window.location.reload()
                 }, 500)
        } else {
          $.push.transitionOut($page, {
            beforeTransition: function() {
              that.$agentWithdraw.off()
                .parent().empty().html(itemDomString)
              that.$agentWithdraw.parent().trigger("refresh")
            }
          })
        }
      })
    },

    compilewithdrawPage: function() {

      var that = this,
        deferPage = $.Deferred(),
        defer = this.requestwithdraw()

      $.when(defer).done(function(data) {

        var $page = $(JST.agentWithdrawTpl({
        	'kickbackTotal': data.kickbackTotal,
        	'couldWithdraw': data.couldWithdraw
        }))

        // Store request withdraw
        that.modal.kickbackTotal = data.kickbackTotal,
        that.modal.couldWithdraw = data.couldWithdraw;
        
        // Bind submit
        that.bindagentWithdrawPageEvent($page)

        // Set defer as resolve status
        deferPage.resolve($page)
      })

      return deferPage.promise()
    },
                
    requestwithdraw: function() {
      var defer = $.Deferred(),
              url = this._CTX_.root + '/profile-online-agent/agent-record/withdraw'

          // Show request indicator
          $.showIndicator()

          // Request for rounds data
          $.ajax({
            'type': 'GET',
            'url': url,
            'cache': false
          }).done(function(response) {
            $.hideIndicator()  // Hide request indicator
            utils.processResult(response, function(result) {
                defer.resolve(result.data)
            }, function(result) {
                $.toast(result.message || '获取账户信息失败')
            })
          })

        return defer.promise()
    },

    bindagentWithdrawPageEvent: function($page) {
      var that = this,
        $offer = $('input[name="offer"]', $page)
        
        $page.on('click', '#submit', function(e) {

          e.preventDefault()
          
          var couldWithdraw = that.modal.couldWithdraw;
          
          var flag = true;

          var offer = $offer.val().trim();
          if(offer == "") {
        	  flag = false;
        	  $.toast("提现金额不能为空");
          } else if(offer > couldWithdraw) {
        	  flag = false;
        	  $.toast("提现金额不能大于" + couldWithdraw)
          }
          
          if(flag) {
        	  var submiting = that.submitwithdraw(offer)
              $.when(submiting).done(function(data) {
                that.$agentWithdraw.trigger('edited:profile:withdraw', [$page, that.offer])
              })
          }
          
        })
    },

    submitwithdraw: function(offer) {
      var that = this, defer = $.Deferred()
      $.showIndicator()
      $.post(that._CTX_.root + "/profile-online-agent/agent-record/withdraw", {
        'offer': offer,
        '_method': 'PATCH'
      }).done(function(response) {
        $.hideIndicator()
              utils.processResult(response, function(result) {
                defer.resolve(result.data)
            }, function(result) {
                $.toast(result.message || '修改失败')
                defer.resolve(result.data)
            })
      })
    }
  }
})